// pages/todo.js
import Toast from '@vant/weapp/toast/toast'
// 获取应用实例
const app = getApp()
const baseUrl = getApp().globalData.baseUrl
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: getApp().globalData.userInfo,
    phoneNum: getApp().globalData.phoneNum,
    active: 0,
    dialogId: '',
    // 待处置事件  待处置事件
    eventData: {
      total: 0,
      pageNum: 1,
      pageSize: 100,
      list: []
    },
    // 待处置事件  已处置事件
    eventData1: {
      total: 0,
      pageNum: 1,
      pageSize: 100,
      list: []
    },
    // total0: wx.getStorageSync('total0')? wx.getStorageSync('total0') : 0,
    // total1: wx.getStorageSync('total1')? wx.getStorageSync('total1') : 0,
    total0: 0,
    total1: 0,
    isDialogShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPendingeventList(0)
    this.getPendingeventList(1)
    let that = this
    getApp().watchUserInfo(that.watchBackUserInfo)
    getApp().watchPhoneNum(that.watchBackPhoneNum)
  },
  watchBackUserInfo(userInfo) {
    console.log('监听到的数据userInfo：' + userInfo)
    let that = this
    that.setData({
      userInfo: userInfo
    })
  },
  watchBackPhoneNum(phoneNum) {
    console.log('监听到的数据phoneNum：' + phoneNum)
    let that = this
    that.setData({
      phoneNum: phoneNum
    })
  },
  // 事件待办  处置详情
  goDetail(item) {
    console.log(item, '009')
    let type = item.currentTarget.dataset.status.eventStatus  // 0签收  1处置
    let todoId = item.currentTarget.dataset.status.todoId
    let dealwith = 'dealwith1'  // 处置设置状态
    if(type == 0) {
      return
    }
    wx.navigateTo({
      url: `../detailother/detailother?type=${type}&todoId=${todoId}&dealwith=${dealwith}`,
    })
  },
  goDetail1(item) {
    let type = item.currentTarget.dataset.status.eventStatus
    let todoId = item.currentTarget.dataset.status.todoId
    let dealwith = 'dealwith2' // 处置已设置状态
    wx.navigateTo({
      url: `../dealwithDetail/dealwithDetail?type=${type}&id=${todoId}&dealwith=${dealwith}`,
    })
  },

  // 弹窗信息
  getLaQun() {
    let that = this
    Toast({
      message: "数字助理将为您联系群众拉群",
      duration:3000
    })
    let todoId = this.data.dialogId
    let url1 = `${baseUrl}/api/eventTodo/pull/${todoId}`
    wx.request({
      url: url1,
      method: 'post',
      header: {
        'token': wx.getStorageSync('token')
      },
      success: (resLa) => {
        if(resLa.data.code == 0) {
          // wx.showToast({
          //   title: '拉群成功',
          //   icon: 'success',
          //   duration: 2000
          // })
          that.getPendingeventList(that.data.active)
        } else {
          wx.showToast({
            title: '拉群失败',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },

  onClose() {
    this.setData({ isDialogShow: false });
    this.getPendingeventList(this.data.active)
  },
  // 签收
  signFor(item) {
    // this.setData({
    //   isDialogShow: true,
    //   dialogId: item.currentTarget.dataset.status.todoId
    // })
    // return
    let todoId = item.currentTarget.dataset.status.todoId
    let that = this
    let url = `${baseUrl}/api/eventTodo/confirm/${todoId}`
    console.log(wx.getStorageSync('token'), 'token')
    wx.request({
      url: url,
      method: 'POST',
      header: {
        "token": wx.getStorageSync('token'),
        'content-type': 'application/json'
      },
      success: function(res) {
        that.setData({
         eventData: {
           pageNum: 1,
           pageSize: 100,
           list: []
         },
        })
        if(res.data.code != 0) {
         Toast({
           message: res.data.msg,
           duration:2000
          })
          that.getPendingeventList(that.data.active)
          return
        } else if(res.data.code == 0) {
          that.setData({ 
            isDialogShow: true,
            dialogId: item.currentTarget.dataset.status.todoId
          });
          // wx.showModal({
          //   title: '签收成功',
          //   content: '您是否要与群众拉群沟通？',
          //   confirmText: '拉群',
          //   cancelText:'不拉群',
          //   success(res1) {
          //     if(res1.confirm) {
          //       Toast({
          //         message: "数字助理将为您联系群众拉群",
          //         duration:3000
          //       })
          //       let url1 = `${baseUrl}/api/eventTodo/pull/${todoId}`
          //       wx.request({
          //         url: url1,
          //         method: 'post',
          //         header: {
          //           'token': wx.getStorageSync('token')
          //         },
          //         success: (resLa) => {
          //           if(resLa.data.code == 0) {
          //             wx.showToast({
          //               title: '拉群成功',
          //               icon: 'success',
          //               duration: 2000
          //             })
          //           } else {
          //             wx.showToast({
          //               title: '拉群失败',
          //               icon: 'error',
          //               duration: 2000
          //             })
          //           }
          //         }
          //       })
          //     }
          //     that.getPendingeventList(that.data.active)
          //   }
          // })
        }
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },
  // tab切换
  changeTab(e) {
    // this.setData({
    //   active: e.detail.index
    // })
    if(e.detail.index == 0) {
      this.setData({
        active: e.detail.index,
        eventData: {
          pageNum: 1,
          pageSize: 100,
        },
      })
    } else if(e.detail.index == 1) {
      this.setData({
        active: e.detail.index,
        eventData1: {
          pageNum: 1,
          pageSize: 100,
        },
      })
    }
    this.getPendingeventList(this.data.active)
  },
  // 待处置事件
  getPendingeventList(type) {
    let that = this
    let url = `${baseUrl}/api/eventTodo/list`
    let data0 = {
      pageNum: that.data.eventData.pageNum,
      pageSize: that.data.eventData.pageSize,
      type: type
    }
    let data1 = {
      pageNum: that.data.eventData1.pageNum,
      pageSize: that.data.eventData1.pageSize,
      type: type
    }
    let params;
    if(type == 0) {
      params = data0
    } else {
      params = data1
    }
    wx.request({
      url: url,
      data: params,
      // data: {
      //   pageNum: that.data.eventData.pageNum,
      //   pageSize: that.data.eventData.pageSize,
      //   type: type
      // },
      header: {
        'content-type': 'application/json',
        "token": wx.getStorageSync('token')
        // "token": "fdb07f065c214892899dcc691841fa4e"
      },
      success: function(res) {
        if (type == 0) {
          // wx.setStorageSync('total0', res.data.list.total)
          that.setData({
            total0: res.data.list.total,
            eventData: {
              list: res.data.list.list
            }
          })
        } else {
          // wx.setStorageSync('total1', res.data.list.total)
          that.setData({
            total1: res.data.list.total,
            eventData1: {
              list: res.data.list.list
            }
          })
        }
      },
      fail: function(err) {
        wx.showToast({
          title: '请求失败',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },



  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
   onShareAppMessage(res) {
     console.log(res, 'todo')
     if(res.target.dataset.share ==="signfor") {
      return {
        title: '签收成功',
        path: '/pages/todo/todo' // 路径，传递参数到指定页面。
      }
     } else {
      return {
        title: '数字人慧勤务平台',
        desc: '数字人慧勤务平台',
        path: '/pages/todo/todo' // 路径，传递参数到指定页面。
       }
     }
  }
})